﻿using System;
using System.Collections.Generic;
using log4net.Appender;

namespace HashFoo.MigratorFooTests
{
    public class LogListenerAppender : IAppender
    {
        public LogListenerAppender()
        {
            this.LevelFilter = log4net.Core.Level.All;
            this.LogNameFilter = new List<string>();
        }

        public LogListenerAppender(Action<log4net.Core.LoggingEvent> onlog)
            : this()
        {
            OnLog += onlog;
        }

        public log4net.Core.Level LevelFilter { get; set; }
        public IList<string> LogNameFilter { get; set; }

        public event Action<log4net.Core.LoggingEvent> OnLog;

        #region IAppender Members

        public void Close()
        {
            // do nothing
        }

        public void DoAppend(log4net.Core.LoggingEvent loggingEvent)
        {
            if (loggingEvent.Level < LevelFilter)
            {
                return;
            }

            if (LogNameFilter.Count > 0 && !LogNameFilter.Contains(loggingEvent.LoggerName))
            {
                return;
            }

            if (this.OnLog != null)
            {
                this.OnLog(loggingEvent);
            }
        }

        public string Name
        {
            get
            {
                return "LogListenerAppender";
            }
            set
            {

            }
        }

        #endregion
    }
}